<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>学习管理工具</title>
    <!-- 确保Font Awesome正确引入 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* 基础样式与苹果设计风格统一 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
        }
        
        body {
            background-color: #f5f5f7;
            color: #333;
            line-height: 1.6;
            padding: 20px;
            max-width: 1000px;
            margin: 0 auto;
        }
        
        header {
            text-align: center;
            margin: 30px 0 50px;
        }
        
        h1 {
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 10px;
            color: #1d1d1f;
        }
        
        h2 {
            font-size: 22px;
            font-weight: 500;
            margin-bottom: 20px;
            color: #1d1d1f;
        }
        
        .subtitle {
            color: #6e6e73;
            font-size: 16px;
        }
        
        /* 模块容器样式 */
        .module {
            background-color: white;
            border-radius: 18px;
            padding: 30px;
            margin-bottom: 30px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
            transition: transform 0.2s ease, box-shadow 0.2s ease;
        }
        
        .module:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
        }
        
        /* 倒计时和计时器通用数字容器样式 */
        .time-container {
            display: flex;
            justify-content: center;
            gap: 20px;
            flex-wrap: wrap;
            margin: 30px 0;
        }
        
        .time-item {
            text-align: center;
            flex: 1;
            min-width: 100px;
        }
        
        .time-number {
            font-size: 50px;
            font-weight: 600;
            color: #007aff;
            margin-bottom: 8px;
            line-height: 1;
        }
        
        .time-label {
            font-size: 14px;
            color: #6e6e73;
        }
        
        /* 毫秒显示样式 */
        .milliseconds {
            font-size: 24px;
            color: #6e6e73;
            font-weight: 500;
            margin-top: 10px;
            text-align: center;
        }
        
        /* 计时器模块样式 */
        .timer-display-container {
            text-align: center;
        }
        
        /* 按钮样式 */
        .btn {
            background-color: #f5f5f7;
            border: none;
            border-radius: 12px;
            padding: 12px 24px;
            font-size: 16px;
            font-weight: 500;
            color: #1d1d1f;
            cursor: pointer;
            transition: all 0.2s ease;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
        }
        
        .btn:hover {
            background-color: #ebebeb;
        }
        
        .btn-primary {
            background-color: #007aff;
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #0066cc;
        }
        
        .btn-danger {
            background-color: #ff3b30;
            color: white;
        }
        
        .btn-danger:hover {
            background-color: #cc0000;
        }
        
        /* 删除按钮样式 - 确保显示 */
        .delete-btn {
            background: none;
            border: none;
            color: #6e6e73;
            cursor: pointer;
            padding: 6px;
            border-radius: 6px;
            display: inline-flex;
            align-items: center;
            justify-content: center;
            transition: all 0.2s ease;
        }
        
        .delete-btn:hover {
            color: #ff3b30;
            background-color: #f5f5f7;
        }
        
        /* 学习记录模块样式 */
        .study-controls {
            display: flex;
            gap: 15px;
            margin-bottom: 30px;
            flex-wrap: wrap;
        }
        
        .input-field {
            flex: 1;
            min-width: 200px;
            padding: 12px 16px;
            border: 1px solid #d2d2d7;
            border-radius: 12px;
            font-size: 16px;
            transition: border-color 0.2s ease;
        }
        
        .input-field:focus {
            outline: none;
            border-color: #007aff;
        }
        
        .records-container {
            margin-top: 30px;
        }
        
        .records-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        
        .records-table th,
        .records-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #eaeaea;
            font-size: 14px;
        }
        
        .records-table th {
            color: #6e6e73;
            font-weight: 500;
        }
        
        .records-table tr:hover {
            background-color: #f9f9f9;
        }
        
        .records-table .action-column {
            width: 60px;
            text-align: center;
        }
        
        .export-btn-container {
            text-align: right;
            margin-top: 20px;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            .time-number {
                font-size: 40px;
            }
            
            .module {
                padding: 20px;
            }
            
            .records-table {
                font-size: 12px;
            }
            
            .records-table th,
            .records-table td {
                padding: 8px 10px;
            }
            
            /* 在小屏幕上隐藏部分列 */
            .hide-on-mobile {
                display: none;
            }
        }
        
        @media (max-width: 480px) {
            body {
                padding: 10px;
            }
            
            .time-container {
                gap: 10px;
            }
            
            .time-item {
                min-width: 70px;
            }
            
            .time-number {
                font-size: 32px;
            }
            
            .btn {
                padding: 10px 16px;
                font-size: 14px;
            }
        }
    </style>
</head>
<body>
    <header>
        <h1>学习管理工具</h1>
        <p class="subtitle">专注于高效学习与时间管理</p>
    </header>

    <!-- 目标倒计时模块 -->
    <section class="module">
        <h2>目标倒计时</h2>
        <p>距离 2025年9月13日 还剩：</p>
        <div class="time-container">
            <div class="time-item">
                <div id="days" class="time-number">000</div>
                <div class="time-label">天</div>
            </div>
            <div class="time-item">
                <div id="hours" class="time-number">00</div>
                <div class="time-label">时</div>
            </div>
            <div class="time-item">
                <div id="minutes" class="time-number">00</div>
                <div class="time-label">分</div>
            </div>
            <div class="time-item">
                <div id="seconds" class="time-number">00</div>
                <div class="time-label">秒</div>
            </div>
        </div>
    </section>

    <!-- 通用计时器模块 -->
    <section class="module">
        <h2>学习计时器</h2>
        <div class="timer-display-container">
            <div class="time-container">
                <div class="time-item">
                    <div id="timer-hours" class="time-number">00</div>
                    <div class="time-label">时</div>
                </div>
                <div class="time-item">
                    <div id="timer-minutes" class="time-number">00</div>
                    <div class="time-label">分</div>
                </div>
                <div class="time-item">
                    <div id="timer-seconds" class="time-number">00</div>
                    <div class="time-label">秒</div>
                </div>
            </div>
            <div id="timer-milliseconds" class="milliseconds">00</div>
        </div>
    </section>

    <!-- 学习记录系统模块 -->
    <section class="module">
        <h2>学习记录</h2>
        <div class="study-controls">
            <input type="text" id="subject-input" class="input-field" placeholder="输入学习科目（如：数学、英语）">
            <button id="start-study-btn" class="btn btn-primary">
                <i class="fas fa-play"></i> 开始学习
            </button>
            <button id="end-study-btn" class="btn btn-danger" disabled>
                <i class="fas fa-stop"></i> 结束学习
            </button>
        </div>
        
        <div class="records-container">
            <h3>历史记录</h3>
            <table class="records-table">
                <thead>
                    <tr>
                        <th>序号</th>
                        <th>学习科目</th>
                        <th class="hide-on-mobile">开始时间</th>
                        <th class="hide-on-mobile">结束时间</th>
                        <th>总耗时</th>
                        <th class="action-column">操作</th>
                    </tr>
                </thead>
                <tbody id="records-body">
                    <!-- 记录将通过JavaScript动态添加 -->
                    <tr>
                        <td colspan="6" style="text-align: center; color: #6e6e73;">暂无学习记录</td>
                    </tr>
                </tbody>
            </table>
            
            <div class="export-btn-container">
                <button id="export-btn" class="btn">
                    <i class="fas fa-download"></i> 导出数据 (CSV)
                </button>
            </div>
        </div>
    </section>

    <script>
        // 全局变量
        let timerInterval;
        let startTime = 0;
        let elapsedTime = 0;
        let isRunning = false;
        let studyStartTime = null;
        let studyRecords = JSON.parse(localStorage.getItem('studyRecords')) || [];

        // DOM元素
        const daysEl = document.getElementById('days');
        const hoursEl = document.getElementById('hours');
        const minutesEl = document.getElementById('minutes');
        const secondsEl = document.getElementById('seconds');
        const timerHoursEl = document.getElementById('timer-hours');
        const timerMinutesEl = document.getElementById('timer-minutes');
        const timerSecondsEl = document.getElementById('timer-seconds');
        const timerMillisecondsEl = document.getElementById('timer-milliseconds');
        const subjectInputEl = document.getElementById('subject-input');
        const startStudyBtnEl = document.getElementById('start-study-btn');
        const endStudyBtnEl = document.getElementById('end-study-btn');
        const recordsBodyEl = document.getElementById('records-body');
        const exportBtnEl = document.getElementById('export-btn');

        // 初始化页面
        function init() {
            updateCountdown();
            setInterval(updateCountdown, 1000);
            renderRecords();
            
            // 事件监听
            startStudyBtnEl.addEventListener('click', startStudy);
            endStudyBtnEl.addEventListener('click', endStudy);
            exportBtnEl.addEventListener('click', exportRecords);
        }

        // 1. 目标倒计时功能
        function updateCountdown() {
            const targetDate = new Date(2025, 8, 13); // 月份从0开始，9月是8
            const now = new Date();
            const diff = targetDate - now;
            
            if (diff <= 0) {
                daysEl.textContent = '000';
                hoursEl.textContent = '00';
                minutesEl.textContent = '00';
                secondsEl.textContent = '00';
                return;
            }
            
            const days = Math.floor(diff / (1000 * 60 * 60 * 24));
            const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
            const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
            const seconds = Math.floor((diff % (1000 * 60)) / 1000);
            
            // 格式化数字为两位数或三位数
            daysEl.textContent = days.toString().padStart(3, '0');
            hoursEl.textContent = hours.toString().padStart(2, '0');
            minutesEl.textContent = minutes.toString().padStart(2, '0');
            secondsEl.textContent = seconds.toString().padStart(2, '0');
        }

        // 2. 通用计时器功能
        function startTimer() {
            if (!isRunning) {
                startTime = Date.now() - elapsedTime;
                timerInterval = setInterval(updateTimer, 10);
                isRunning = true;
            }
        }

        function pauseTimer() {
            if (isRunning) {
                clearInterval(timerInterval);
                elapsedTime = Date.now() - startTime;
                isRunning = false;
            }
        }

        function resetTimer() {
            clearInterval(timerInterval);
            startTime = 0;
            elapsedTime = 0;
            isRunning = false;
            
            // 重置显示
            timerHoursEl.textContent = '00';
            timerMinutesEl.textContent = '00';
            timerSecondsEl.textContent = '00';
            timerMillisecondsEl.textContent = '00';
        }

        function updateTimer() {
            const currentTime = Date.now();
            elapsedTime = currentTime - startTime;
            
            // 计算小时、分钟、秒和毫秒
            const hours = Math.floor(elapsedTime / (1000 * 60 * 60));
            const minutes = Math.floor((elapsedTime % (1000 * 60 * 60)) / (1000 * 60));
            const seconds = Math.floor((elapsedTime % (1000 * 60)) / 1000);
            const milliseconds = Math.floor((elapsedTime % 1000) / 10);
            
            // 更新显示
            timerHoursEl.textContent = hours.toString().padStart(2, '0');
            timerMinutesEl.textContent = minutes.toString().padStart(2, '0');
            timerSecondsEl.textContent = seconds.toString().padStart(2, '0');
            timerMillisecondsEl.textContent = milliseconds.toString().padStart(2, '0');
        }

        // 3. 学习记录系统功能
        function startStudy() {
            const subject = subjectInputEl.value.trim();
            if (!subject) {
                alert('请输入学习科目');
                return;
            }
            
            // 重置计时器并开始
            resetTimer();
            startTimer();
            
            // 记录开始时间
            studyStartTime = new Date();
            
            // 更新按钮状态
            startStudyBtnEl.disabled = true;
            endStudyBtnEl.disabled = false;
            subjectInputEl.disabled = true;
        }

        function endStudy() {
            if (!studyStartTime) return;
            
            // 暂停计时器
            pauseTimer();
            
            // 获取学习信息
            const subject = subjectInputEl.value.trim();
            const endTime = new Date();
            const duration = elapsedTime;
            
            // 格式化时间显示
            const formatTime = (date) => {
                return date.toLocaleString('zh-CN', {
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    hour: '2-digit',
                    minute: '2-digit',
                    second: '2-digit'
                });
            };
            
            // 格式化时长显示
            const formatDuration = (ms) => {
                const seconds = Math.floor(ms / 1000) % 60;
                const minutes = Math.floor(ms / (1000 * 60)) % 60;
                const hours = Math.floor(ms / (1000 * 60 * 60));
                
                return `${hours.toString().padStart(2, '0')}:${
                    minutes.toString().padStart(2, '0')}:${
                    seconds.toString().padStart(2, '0')}`;
            };
            
            // 创建新记录
            const newRecord = {
                id: Date.now(), // 使用时间戳作为唯一ID
                subject: subject,
                startTime: studyStartTime,
                endTime: endTime,
                duration: duration,
                startTimeStr: formatTime(studyStartTime),
                endTimeStr: formatTime(endTime),
                durationStr: formatDuration(duration)
            };
            
            // 添加到记录列表并保存
            studyRecords.unshift(newRecord); // 添加到列表开头
            saveRecords();
            renderRecords();
            
            // 重置状态
            studyStartTime = null;
            subjectInputEl.value = '';
            
            // 更新按钮状态
            startStudyBtnEl.disabled = false;
            endStudyBtnEl.disabled = true;
            subjectInputEl.disabled = false;
        }

        // 删除记录功能
        function deleteRecord(index) {
            if (confirm('确定要删除这条学习记录吗？')) {
                studyRecords.splice(index, 1);
                saveRecords();
                renderRecords();
            }
        }

        function saveRecords() {
            localStorage.setItem('studyRecords', JSON.stringify(studyRecords));
        }

        function renderRecords() {
            recordsBodyEl.innerHTML = '';
            
            if (studyRecords.length === 0) {
                const emptyRow = document.createElement('tr');
                emptyRow.innerHTML = '<td colspan="6" style="text-align: center; color: #6e6e73;">暂无学习记录</td>';
                recordsBodyEl.appendChild(emptyRow);
                return;
            }
            
            studyRecords.forEach((record, index) => {
                const row = document.createElement('tr');
                row.innerHTML = `
                    <td>${index + 1}</td>
                    <td>${record.subject}</td>
                    <td class="hide-on-mobile">${record.startTimeStr}</td>
                    <td class="hide-on-mobile">${record.endTimeStr}</td>
                    <td>${record.durationStr}</td>
                    <td class="action-column">
                        <button onclick="deleteRecord(${index})" class="delete-btn" title="删除记录">
                            <i class="fas fa-trash"></i>
                        </button>
                    </td>
                `;
                recordsBodyEl.appendChild(row);
            });
        }

        function exportRecords() {
            if (studyRecords.length === 0) {
                alert('暂无记录可导出');
                return;
            }
            
            // 创建CSV内容
            let csvContent = '序号,学习科目,开始时间,结束时间,总耗时\n';
            
            studyRecords.forEach((record, index) => {
                csvContent += `${index + 1},${record.subject},${record.startTimeStr},${record.endTimeStr},${record.durationStr}\n`;
            });
            
            // 创建下载链接
            const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.setAttribute('href', url);
            
            // 文件名包含当前日期
            const today = new Date();
            const fileName = `学习记录_${today.getFullYear()}${(today.getMonth() + 1).toString().padStart(2, '0')}${today.getDate().toString().padStart(2, '0')}.csv`;
            
            a.setAttribute('download', fileName);
            a.style.display = 'none';
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        }

        // 暴露删除函数到window
        window.deleteRecord = deleteRecord;

        // 页面加载完成后初始化
        window.addEventListener('DOMContentLoaded', init);
    </script>
</body>
</html>
